from tkinter.filedialog import askopenfilename
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model
from PIL import Image
# 步骤1：直接指定图片路径
img_path = r'E:\PythonProject\集成医疗诊断平台\testdata\f6c65289d4bb05c856e827010e7c80d.bmp'
print('选择的图片：', img_path)
# 步骤2：图片预处理

def preprocess_image(img_path, target_size=(224,224)):
    img = Image.open(img_path).convert('RGB')
    img = img.resize(target_size)
    img_array = np.array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)
    return img_array

img_array = preprocess_image(img_path, target_size=(224,224))

# 步骤3：加载模型（换成你的模型）
model = load_model('../model/best_vgg16_finetune.h5')


index_to_class = {
    0: "im_Dyskeratotic",
    1: "im_Koilocytotic",
    2: "im_Metaplastic",
    3: "im_Parabasal",
    4: "im_Superficial-Intermediate"
}

# 步骤5：预测
pred = model.predict(img_array)
predicted_class_index = np.argmax(pred, axis=1)[0]
predicted_class_name = index_to_class[predicted_class_index]

print(f"预测结果为：{predicted_class_name}")